package light.mvc.service.item.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import light.mvc.dao.BaseDaoI;
import light.mvc.model.item.TchemicalExam;
import light.mvc.page.base.PageFilter;
import light.mvc.page.item.ChemicalExam;
import light.mvc.service.item.ChemicalExamServiceI;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ChemicalExamServiceImpl implements ChemicalExamServiceI {

	@Autowired
	private BaseDaoI<TchemicalExam> chemicalExamDao;

	@Override
	public void add(ChemicalExam chemicalExam) {
		TchemicalExam t = new TchemicalExam();
		BeanUtils.copyProperties(chemicalExam, t);
		chemicalExamDao.save(t);
	}

	@Override
	public void delete(Integer id) {
		TchemicalExam t = chemicalExamDao.get(TchemicalExam.class, id);
		chemicalExamDao.delete(t);
	}

	@Override
	public void edit(ChemicalExam chemicalExam) {
		TchemicalExam t = chemicalExamDao.get(TchemicalExam.class,chemicalExam.getId());
		BeanUtils.copyProperties(chemicalExam, t);
		chemicalExamDao.update(t);
	}

	@Override
	public ChemicalExam get(Integer id) {
		TchemicalExam t = chemicalExamDao.get(TchemicalExam.class,id);
		ChemicalExam chemicalExam = new ChemicalExam();
		BeanUtils.copyProperties(t, chemicalExam);
		
		return chemicalExam;
	}
	
	@Override
	public List<ChemicalExam> dataGrid(ChemicalExam chemicalExamSome, PageFilter ph) {
		List<ChemicalExam> chemicalExams = new ArrayList<ChemicalExam>();
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from TchemicalExam t ";
		List<TchemicalExam> list = chemicalExamDao.find(hql + whereHql(chemicalExamSome, params) + orderHql(ph), params, ph.getPage(), ph.getRows());
		for (TchemicalExam t : list) {
			ChemicalExam chemicalExam = new ChemicalExam();
			BeanUtils.copyProperties(t, chemicalExam);
			chemicalExams.add(chemicalExam);
		}
		return chemicalExams;
	}
	
	@Override
	public Long count(ChemicalExam chemicalExam, PageFilter ph) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from TchemicalExam t ";
		return chemicalExamDao.count("select count(*) " + hql + whereHql(chemicalExam, params), params);
	}
	
	private String whereHql(ChemicalExam chemicalExam, Map<String, Object> params) {
		String hql = "";
		if (chemicalExam != null) {
			hql += " where 1=1 ";
			if (chemicalExam.getChemicalExamItem()!= null) {
				hql += " and t.chemicalExamItem like :chemicalExamItem";
				params.put("name", "%" + chemicalExam.getChemicalExamItem() + "%");
			}
		}
		return hql;
	}

	private String orderHql(PageFilter ph) {
		String orderString = "";
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {
			orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}
	
	@Override
	public ChemicalExam getByItem(ChemicalExam chemicalExamSome) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("chemicalExamItem", chemicalExamSome.getChemicalExamItem());
		TchemicalExam t = chemicalExamDao.get("from TchemicalExam t where t.chemicalExamItem = :chemicalExamItem",params);
		ChemicalExam chemicalExam = new ChemicalExam();
		if(t!=null){
			BeanUtils.copyProperties(t, chemicalExam);
		}else{
			return null;
		}
		return chemicalExam;
	}
	
	@Override
	public List<ChemicalExam> list() {
		List<ChemicalExam> chemicalExams = new ArrayList<ChemicalExam>();
		String hql = " from TchemicalExam t ";
		List<TchemicalExam> list = chemicalExamDao.find(hql);
		for (TchemicalExam t : list) {
			ChemicalExam chemicalExam = new ChemicalExam();
			BeanUtils.copyProperties(t, chemicalExam);
			chemicalExams.add(chemicalExam);
		}
		return chemicalExams;
	}
}
